package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class AccountDefaultWapper {
	SqlManager manage;

	public AccountDefaultWapper() {
		if (manage == null || manage.getEnv() != "ymt" + EnvSetup.getEnv() || manage.isClosed()) {
			manage = new SqlManager("ymt" + EnvSetup.getEnv());
		}
	}

	/**
	 * 使用 accountid 查询信息
	 * 
	 * @param accountid
	 * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
	 *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
	 *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
	 *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
	 * @throws InterruptedException
	 */
	public List<Map> selectAccountInfo(String accountid) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Map> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountInfo", accountid);
		manage.commit();
		return pia;
	}

	/**
	 * 使用 userId, accountType查询账户信息
	 * 
	 * @param userId
	 *            用户编号
	 * @param accountType
	 *            账户类型
	 * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
	 *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
	 *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
	 *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
	 * @throws InterruptedException
	 */
	public List<Map> selectAccountInfoByUserId(int userId, int accountType) throws InterruptedException {
		Map m = new HashMap<String, Integer>();
		m.put("userId", userId);
		m.put("accountType", accountType);
		List<Map> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountInfoByUserId", m);
		manage.commit();
		return pia;
	}

	/**
	 * 使用 userId 查询账户信息
	 * 
	 * @param userId
	 * @return {dCreateTime=2016-01-26 10:33:57.263, dUpdateTime=2016-01-26
	 *         10:33:57.263, fAvailAmount=0.00, iAction=0, iCurrencyType=1,
	 *         iUserId=20000000, iAccountType=0, Version=0, fRewardAmount=0.00,
	 *         fFreezeAmount=0.00, sAccountInfoId=00010020000000, sMemo=自动化测试专用}
	 * @throws InterruptedException
	 */
	public List<Map> selectAccountInfoByUserId(int userId) throws InterruptedException {
		Map m = new HashMap<String, Integer>();
		m.put("userId", userId);
		List<Map> pia = null;
		try {
			pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountInfoByUserId", m);
			manage.commit();
		} catch (Throwable e) {
			if (pia == null || pia.size() <= 0) {
				Thread.sleep(1000);
				pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountInfoByUserId", m);
				manage.commit();
			}
		}
//		manage.commit();
		return pia;
	}

	/**
	 * 使用 accountid 查询信息
	 * 
	 * @param accountid
	 * @param ono
	 * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
	 *         Operator=AutoTestOperator,
	 *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
	 *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
	 *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
	 *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
	 *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
	 *         ServerIp=172.16.100.175,
	 *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
	 *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
	 *         ClientIp=172.16.13.71} * @throws InterruptedException
	 */
	public List<YmtAccountEntry> selectAccountEntry(String accountid, String ono) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("accountId", accountid);
		m.put("oNo", ono);
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntry", m);
		manage.commit();
		return pia;
	}

	/**
	 * 动态关联Ymt_AccountEntry ,ymt_accountinfo查询账户流水信息
	 * 
	 * @param map
	 * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
	 *         Operator=AutoTestOperator,
	 *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
	 *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
	 *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
	 *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
	 *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
	 *         ServerIp=172.16.100.175,
	 *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
	 *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
	 *         ClientIp=172.16.13.71} * @throws InterruptedException
	 */
	public List<YmtAccountEntry> selectAccountEntryAndInfo(Map m) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryAndInfo2", m);
		manage.commit();
		return pia;
	}

	/**
	 * 重载方法 使用 accountid 查询信息
	 * 
	 * @param accountid
	 * @param ono
	 * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
	 *         Operator=AutoTestOperator,
	 *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
	 *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
	 *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
	 *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
	 *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
	 *         ServerIp=172.16.100.175,
	 *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
	 *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
	 *         ClientIp=172.16.13.71} * @throws InterruptedException
	 */
	public List<YmtAccountEntry> selectAccountEntry(String accountid) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByAcId", accountid);
		manage.commit();
		return pia;
	}

	/**
	 * 重载方法 使用 userid, bizNo 查询信息
	 * 
	 * @param userid
	 * @param bizNo
	 * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
	 *         Operator=AutoTestOperator,
	 *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
	 *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
	 *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
	 *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
	 *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
	 *         ServerIp=172.16.100.175,
	 *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
	 *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
	 *         ClientIp=172.16.13.71} * @throws InterruptedException
	 */
	public List<YmtAccountEntry> selectAccountEntry(int userId, String bizNo, int accountType) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap();
		m.put("userId", userId);
		m.put("bizNo", bizNo);
		m.put("accountType", accountType);
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByUserid", m);
		manage.commit();
		return pia;
	}

	/**
	 * 关联查询Ymt_AccountEntry,Ymt_AccountInfo2张表 返回前10条记录
	 * 
	 * @return {EntryType=2, Amount=10.00, AvailAmount=10.00,
	 *         Operator=AutoTestOperator,
	 *         BizNo=34267c7d-0218-4f1a-9a77-ee569ba867d3, AppId=AutoTest,
	 *         UpdateTime=2016-01-26 17:49:48.353, UserId=20000000,
	 *         AccountingDate=2016-01-26 17:48:13.0, FreezeAmount=0.00,
	 *         OriginalNo=68e67232-0a86-4d8c-889d-0c78a54c0b8e,
	 *         EntryId=1000001540, BizCode=100001, AccountId=00010020000000,
	 *         ServerIp=172.16.100.175,
	 *         RequestId=2d6131c2-1d22-4f76-8f31-5563ea48057f,
	 *         Memo=自动化测试:Fundin, CreateTime=2016-01-26 17:49:48.353,
	 *         ClientIp=172.16.13.71} * @throws InterruptedException
	 */
	public List<HashMap> selectAccountEntryAndInfo() throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<HashMap> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryAndInfo", null);
		return pia;
	}

	/**
	 * 根据userid查询Ymt_AccountEntry，返回updatetime 最新的一条数据
	 */
	public List<YmtAccountEntry> selectAccountEntryByUserId(int userId) throws InterruptedException {
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByUserid2", userId);
		manage.commit();
		return pia;
	}

	public int selectCountByUserid(int userId) throws InterruptedException {
		List<Map> m = (List<Map>) manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectCountByUserid", userId);
		manage.commit();
		int pia = (Integer) m.get(0).get("count");
		return pia;
	}

	/**
	 * 根据userid查询Ymt_AccountEntry，返回updatetime 最新limit条数据
	 */
	public List<YmtAccountEntry> selectAccountEntryByUseridLimit(String userId, int limit) throws InterruptedException {
		Map m = new HashMap();
		m.put("userId", userId);
		m.put("limit", limit);
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByUseridLimit", m);
		return pia;
	}

	/**
	 * 根据userid查询Ymt_AccountEntry，返回updatetime 最新limit条数据
	 */
	public List<YmtAccountEntry> selectAccountEntryByMap(Map m) throws InterruptedException {
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByMap", m);
		manage.commit();
		return pia;
	}

	/**
	 * 根据userid查询Ymt_AccountEntry，返回updatetime 最新limit条数据
	 */
	public List<YmtAccountEntry> selectAccountEntryByBizNo(Map m) throws InterruptedException {
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByBizNo", m);
		manage.commit();
		return pia;
	}

	/**
	 * 根据originalNo
	 */
	public List<YmtAccountEntry> selectAccountEntryByOriginalNo(String originalNo) throws InterruptedException {
		Thread.sleep(1000);
		Map m = new HashMap();
		m.put("originalNo", originalNo);
		List<YmtAccountEntry> pia = manage.getSelectResults("com.ymttest.database.mapping.AccountDefaultMapper.selectAccountEntryByOriginalNo", m);
		manage.commit();
		return pia;
	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		try {
			/*
			 * List<YmtAccountEntry> list = new
			 * AccountDefaultWapper().selectAccountEntry
			 * ("00010020000000","68e67232-0a86-4d8c-889d-0c78a54c0b8e"); for
			 * (YmtAccountEntry ya : list) { Logger.debug(ya.getAccountid()); }
			 */

			// List<YmtAccountEntry> m=new
			// AccountDefaultWapper().selectAccountEntry(355976173,"f7a44e0ae1464e3391e46e67e608cfd5",0);
			// List<Map> m=new
			// AccountDefaultWapper().selectAccountInfoByUserId(20000002,0);
			Map m = new HashMap();
			m.put("UserId", "21000002");
			m.put("BeginTime", "2016-01-28 18:05:26.237");
			m.put("EndTime", "2016-01-29 22:05:26.237");
			m.put("AccountingType", 1);

			m.put("limit", 20);
			/*
			 * List<HashMap> rm=new
			 * AccountDefaultWapper().selectAccountEntryByMap(m);
			 */

			List<YmtAccountEntry> rm = new AccountDefaultWapper().selectAccountEntryAndInfo(m);

			Logger.debug("msize:" + rm.size());
			// Logger.debug("m:"+FormatUtil.beanToGSONString(rm));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
